﻿<?php
class Longadmin_SupplierController extends Zend_Controller_Action
{
	public function addAction()
	{	
		$form = new Form_UploadFormSupplier();
		$form->setAction('add')
			 ->setMethod('post');
		if($form->image->receive()){
			$_IMAGE = $form->image->getFileName(null,false); 
		}
		$this->view->message = $form;
		if($this->_request->isPost()){
			$db = Zend_Registry::get('db');
			$table = 'supplier';
			$select = $db->select()->from($table)->where('s_ten= ?',$_POST['s_tenText']);
			$search = $db->query($select)->fetchAll();
			if(count($search) ==0){
				$params = array('s_ten'=>$_POST['s_tenText'],
							's_diachi'=> $_POST['s_diachiText'],
							's_link' => $_POST['s_linkText'],
							's_anh' => $_IMAGE);
				$db->insert($table,$params);
				$this->view->message = 'Hoàn Thành, Đã Tạo Thêm Nhà Cung Cấp  '.$_POST['s_tenText'];
			}
			else{ 
				$this->view->message = 'Đã có nhà cung cấp '.$_POST['s_tenText'];
			}
		}
	}
	
	public function updateAction()
	{
		$form = new Form_UploadFormSupplier();
		$db = Zend_Registry::get('db');
		$table = 'supplier';
		$select = $db->select()->from($table)->where('s_id= ?',$_GET['id']);
		$search = $db->query($select)->fetchAll();

		$s_tenText = $form->getElement('s_tenText');
		$s_tenText->setValue($search[0]['s_ten']);
		$s_diachiText = $form->getElement('s_diachiText');
		$s_diachiText->setValue($search[0]['s_diachi']);
		$s_linkText = $form->getElement('s_linkText');
		$s_linkText->setValue($search[0]['s_link']);			
		$this->view->image = $search[0]['s_anh'];
		$this->view->form = $form;
		$form->setAction('update')
			 ->setMethod('post');	
		if($this->_request->isPost()){
			//$select_update = $db->select()->from($table)->where('s_ten= ?',$_POST['s_tenText']);
			//$search_update = $db->query($select)->fetchAll();
			//if(count($search_update) ==0){
				$_IMAGE = $form->image->getFileName(null,false);
				if($_IMAGE != $search[0]["s_anh"]&& $_IMAGE!=null){
					if($form->image->receive()){
						$_IMAGE = $form->image->getFileName(null,false); 
					}
				}
				else {
					$_IMAGE = $search[0]['s_anh'];
				}
				$params = array('s_ten'=>$_POST['s_tenText'],
							's_diachi'=> $_POST['s_diachiText'],
							's_link' => $_POST['s_linkText'],
							's_anh' => $_IMAGE);
				$where = $db->select()->from($table)->where('s_id= ?',$search[0]['s_id']);
				$db->update($table,$params,$where);
				$this->view->message = 'Hoàn Thành, Đã Cập Nhật Nhà Cung Cấp  '.$_POST['s_tenText'];
			//}
		}
		
	}	
	
	public function listAction()
	{
		$rp = $_POST['rp'];
		$page = $_POST['page'];
		//echo "Page :".$page."&&";
		$pageStart = ($page-1)*$rp;
		//echo "Page start:" . $pageStart . " &&&";
		$supModel = new Model_Supplier();
		$totalarray = $supModel->getSupplier();
		$result= $supModel->getSupplierLimit($rp,$pageStart);
		//echo "Count:". count ($result);
		$test = array();
		$i=0;

		foreach($result as $x)
		{
			$test[$i]['s_id'] = "".$x['s_id']."";			
			$test[$i]['s_ten'] = "".$x['s_ten']."";
			$test[$i]['s_diachi'] = "".$x['s_diachi']."";	
			$test[$i]['s_link'] = "".$x['s_link']."";				
			$test[$i]['s_anh'] = "<input type='image' src='/public/images/suppliers/"	.$x['s_anh']. "' width='40' height='40'/>";
			$i++;
		} 

		$data = array();
		$data['page'] = $page;
		$data['total'] = count($totalarray);
		foreach ($test as $x) {
			$data['rows'][] = array(
			'id' => $x['s_id'],
			'cell' => array($x['s_id'],$x['s_ten'], $x['s_diachi'],$x['s_link'], $x['s_anh']));
		}
		
		$this->_helper->viewRenderer->setNoRender();
		echo json_encode($data);		

	}
	
	public function testAction()
	{
		$rp = 4;
		$page = 1;
		echo "Page :".$page."&&";
		$pageStart = ($page-1)*$rp;
		echo "Page start:" . $pageStart . " &&&";
		$supModel = new Model_Supplier();
		$totalarray = $supModel->getSupplier();
		$result= $supModel->getSupplierLimit($rp,$pageStart);
		echo "Count:". count ($result);
		var_dump($result);
	}
}